<?php
/**
 * Klasse zum erstellen eines Dropdown
 *
 * @author		Joachim Ruf, Loresoft Software (http://www.loresoft.de)
 * @license		New BSD License
 * @version		Class for Bootstrap v2.3.2
 *
 * */


Class Bootstrap_Dropdown_Handler
{
	private $arrEntry			= array();
	private $class				= NULL;
	private $text				= NULL;
	private $arrParam			= NULL;


	/**
	 * @param string $_title	: Beschriftung
	 * */
	function __construct ($_text, array $_arrParam = array()) {
		if (!isset($_arrParam['href'])) {
			$_arrParam['href']	= '#';
		}

		$this->text		= $_text;
		$this->arrParam	= $_arrParam;
	}


	private function getParameter ($_class, array $_arrParam) {
		$result	= NULL;

		if (isset($_arrParam['class'])) { // Class-Parameter verbinden
			$_arrParam['class']	= $_arrParam['class'].' '.$_class;
		} else {
			$_arrParam['class']	= $_class;
		}

		foreach ($_arrParam as $key => $val) {
			$result	.= ' '.$key.'="'.$val.'"';
		}

		return $result;
	}


	/**
	 * Liefert eine Zeile zurueck.
	 * @param string $_content	: Auszugebender Content
	 * @param array	$_arrParam	: array('href' => 'url', 'title' => 'Title-Text', 'rel' => 'follow|nofollow|..')
	 * @return Bootstrap_Dropdown_Entry
	 * */
	public function setTitle ($_content, array $_arrParam = array()) {
		return $title = new Bootstrap_Dropdown_Entry($_content, $_arrParam);
	}


	/**
	 * Liefert eine Zeile zurueck.
	 * @param string $_content	: Auszugebender Content
	 * @param array	$_arrParam	: array('href' => 'url', 'title' => 'Title-Text', 'rel' => 'follow|nofollow|..')
	 * @return Bootstrap_Dropdown_Entry
	 * */
	public function addEntry ($_content, array $_arrParam = array()) {
		return $this->arrEntry[] = new Bootstrap_Dropdown_Entry($_content, $_arrParam);
	}


	/**
	 * Liefert eine Zeile zurueck.
	 * @param int $i	: Auszugebende Zeile
	 * @return Bootstrap_Dropdown_Entry
	 * */
	public function get ($i) {
		if (!isset($this->arrEntry[$i])) {
			return NULL;
		}

		return $this->arrEntry[$i];
	}


	/**
	 * @desc Liefert alle Zeilen zurueck.
	 * @return Yapary_Form_Formular_Row[]
	 * */
	public function getList () {
		return $this->arrEntry;
	}


	/**
	 * Loescht einen Eintrag.
	 * @param string $id	: Zu loeschender Eintrag
	 * @return bool
	 * */
	public function delete ($i) {
		if (!isset($this->arrEntry[$i])) {
			return false;
		}

		unset($this->arrEntry[$i]);
		return true;
	}


	/**
	 * @desc Gibt das Grid aus.
	 * @return string
	 * */
	public function display() {
		$result	= NULL;
		$class	= NULL;
		if ($this->class !== NULL) {
			$class	= ' class="'.$this->class.'"';
		}

		if (count($this->arrEntry)) {
			/* @var $oRow Bootstrap_DropDown_Entry */
			foreach ($this->arrEntry as $key => $oRow) {
				$result .=	$oRow->display();
			}
		}


		return '<div class="dropdown">
				<span class="dropdown-toggle" data-toggle="dropdown"><a'.$this->getParameter('', $this->arrParam).'>'.$this->text.'<b class="caret"></b></a></span>
				<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
					'.$result.'
				</ul>
			</div>';
	}


}


?>